package com.villaday.idf.activity;


import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.villaday.idf.R;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.model.SmsEntity;
import com.villaday.idf.model.members;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.PhoneUtil;
import com.villaday.idf.util.SharedPreferencesUtil;
import com.villaday.idf.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 超级会员
 */
public class SvipSetNameActivity extends BaseActivity implements OkHttpUtil.OnDataListener {

    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_finish)
    TextView tvFinish;
    @BindView(R.id.et_userNIckName)
    EditText etUserNIckName;
    @BindView(R.id.img_delete)
    ImageView imgDelete;
    Gson gson = new Gson();


    private String trim;
    private int type;
    private String value;
    private LoadingDialog loadingDialog;


    @Override
    protected int getViewResId() {
        return R.layout.activity_svip_set_name;
    }

    @Override
    protected void init() {
        if (!TextUtils.isEmpty(SharedPreferencesUtil.getRoleInfo(this).getMember().getNickName())) {
            switch (type) {
                case 0: {
                    etUserNIckName.setText(SharedPreferencesUtil.getRoleInfo(this).getMember().getNickName());
                    etUserNIckName.setSelection(SharedPreferencesUtil.getRoleInfo(this).getMember().getNickName().length());//将光标移至文字末尾
                    break;
                }
            }
        } else {
            etUserNIckName.setHint("请输入昵称");
        }
    }


    @OnClick({R.id.img_delete, R.id.tv_cancel, R.id.tv_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_delete:
                etUserNIckName.setHint("");
                etUserNIckName.setText("");
                etUserNIckName.performClick();
                etUserNIckName.setFocusable(true);
                etUserNIckName.setFocusableInTouchMode(true);
                break;


            case R.id.tv_cancel:
                onBackPressed();
                break;

            case R.id.tv_finish:
                PhoneUtil.hideKeyboard(view);
                trim = etUserNIckName.getText().toString().trim();
                switch (type) {
                    case 0: //昵称
                        if (trim == null || "".equals(trim)) {
                            ToastUtil.initToast(this, "请输入昵称");
                        }/* else if (SharedPreferencesUtil.getRoleInfo(this).getMember().getNickName().equals(trim)) {
                            ToastUtil.initToast(this, "请勿输入相同昵称！");
                        }*/ else {
                            loadingDialog = LoadingDialog.newInstance("修改中...");
                            loadingDialog.show(getFragmentManager());
                            Map<String, Object> map = new HashMap<>();
                            OkHttpUtil.postJson(this, Constant.URL.members, new members(3, trim), map, this);
                        }
                }
        }


    }

    @Override
    public void onResponse(String url, String json) {
        if (!TextUtils.isEmpty(json)) {
            dismissLoading();
            LogeUtil.e("members", json);
            SmsEntity se = gson.fromJson(json, SmsEntity.class);
            if (Constant.Strings.SUC.equals(se.getCode())) {
                ToastUtil.initToast(this, "修改成功");
                setResult(RESULT_OK);
                finish();
            } else {
                ToastUtil.initToast(this, se.getMsg());
            }
        }
    }

    @Override
    public void onFailure(String url, String error) {

    }

    private void dismissLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }
}
